This topic will cover the following items 1. Scope of Visibility
2. Creating the Function foo()
3. Logical Flow Control
A function is just a chunk of code, wrapped within curly brakets, and given a name.
The contents of a function may be:
- A few lines of code or hundreds,
- Calls to other functions,
- Accepting variables as argument input (or not), and
- Returning some object (or not).
Variables within a function are ‘localized’ to within that variable alone. This allows us to:
df and x are common ones we’ve used thus far).Define a function with a variable x inside the function.
Then examine a similarly named variable with a different assigned value before and after calling that function.
Within the function, the variable represents the value inside the function.
And after the function is called, examine the value of the variable.
NOTICE: before and after function are in “global variable scope, whereas inside the function, the variable is localized within the boundaries of the function itself.”
The global scope is shown in RStudio on the Environment tab.
You can also see what is in the environment by asking to list all variables and functions using the ls() function.
When we make functions, we do so by assigning it the result of the call to the function() function.
While some functions do not take input variables, many require it. To pass variables into the function scope, we identify them in the
If you do not give the function arguments it asks for, it complains loudly.
We can put in default values for arguments by assigning values in the function parentheses.
The reason to put in a default value is to make sure that you can easily accomodate everyone while minimizing the potential for errors.
We must be explicit about wanting to send something back to the caller using the return() function.
It is very convenient to be able to execute come code under certain conditions and another set of code under other conditions.
Always verify that your code produces the correct answer, especially for boundary values.
Sometimes we have several different conditions that we wish to evaluate.
There are so many cases where we use the dichotomous if/else workflow, that we can simplify this code
[1] 20 1 10 16 14 14 1 6 5 7 22 1 8 12 18 13 9 2
There are times when it is very helpful if we can loop over a collection of things. These may be elements in a vector, rows in a data.frame, cycling through files on your computer to load in different data components, or to manipulate and analyze tweets harvested from the internet.
Determine which values are odd and which are even.
[1] TRUE
[1] FALSE
[1] TRUE
[1] FALSE
Notice: how the index number in the code is incremented by 1 for each value.
for() loopThe for() loop does this.
Instead of using the index number to reference a value in a vector, we can refernce the set of items within the vector directly.
Applying FunctionsOnce we have a function, we can then iterate over values using apply().
Notice that the return is a list…
The output from lapply() is a list, whose length is the same as that of the original data. However, it is sometimes necessary to just get back a vector of values for the results instead of futzing around with a list.
Lazy programmers make small code.